//
//  JFCountdownHelper.m
//  JFBase
//
//  Created by Alan Wang on 2019/5/14.
//  Copyright © 2019 Alan Wang. All rights reserved.
//

#import "JFCountdownHelper.h"

@implementation JFCountdownHelper
+(void)countdownTimeInterval:(double)timeInterval action:(CountdownActionBlock)block{
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer =dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0, 0, queue);
    dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), (uint64_t)( timeInterval*NSEC_PER_SEC), 0);
    dispatch_source_set_event_handler(timer, ^{
  
            dispatch_async(dispatch_get_main_queue(), ^{
                if (block) {
                   BOOL flag =  block(timer);
                    if (flag) {
                      dispatch_source_cancel(timer);
                    }
                }else{
                    dispatch_source_cancel(timer);
                }
            });

    });
    // 启动定时器
    dispatch_resume(timer);

}
@end
